/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import helper.NewHibernateUtil;
import java.util.Date;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import pojo.AddHistory;
import pojo.Added;
import pojo.CuahangProduct;
import pojo.Personnel;
import pojo.Product;
import pojo.Store;
import pojo.StoreHistory;
import pojo.StoreProduct;
import pojo.StoreProductId;

/**
 *
 * @author QTuan
 */
public class DAOStore {
    private Session session;
    public DAOStore()
    {
        SessionFactory factory = NewHibernateUtil.getSessionFactory();
        this.session = factory.getCurrentSession();
    }
    public boolean setStore(Store billStore,List<Product> listProduct,List<Integer> dsSoLuongthem,List<CuahangProduct> dscuahangSanPham)
    {
         try
        {
            session.getTransaction().begin();
            session.save(billStore);
            Criteria c = session.createCriteria(Store.class);
            c.addOrder(Order.desc("id"));
            c.setMaxResults(1);
            Store result = (Store)c.uniqueResult();
            
            for(int i=0;i<listProduct.size();i++)
            {
                StoreProductId tempid = new StoreProductId(result.getId(), listProduct.get(i).getId());
                StoreProduct temp = new StoreProduct(tempid);
                session.save(temp);
            }
            for(int i=0;i<dsSoLuongthem.size();i++)
            {
                CuahangProduct cuahangsanpham = dscuahangSanPham.get(i);
                cuahangsanpham.setCount(cuahangsanpham.getCount()+dsSoLuongthem.get(i));
                session.update(cuahangsanpham);
            }
            session.flush();
            session.getTransaction().commit();
             StoreHistory storehistory = new StoreHistory();
            storehistory.setStore(billStore);
            storehistory.setAction("Tạo mới xuất kho cho cửa hàng");
            storehistory.setDtime(new Date());
            storehistory.setAccount(billStore.getPerAccount());
            DAOStoreHistory daohis = new DAOStoreHistory();
            daohis.setStoreHistory(storehistory);
            DAOProduct daoproduct = new DAOProduct();
            for(int i=0;i<listProduct.size();i++)
            {
                daoproduct.updateProduct(listProduct.get(i));
            }
            
            return true;
        }catch(Exception e)
        {
            if(session.getTransaction().isActive())
            {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return false;
    }
    public int getmaxID()
    {
        try{
            session.getTransaction().begin();
            Criteria c = session.createCriteria(Store.class);
            c.addOrder(Order.desc("id"));
            c.setMaxResults(1);
            Store result = (Store)c.uniqueResult();
            session.flush();
            session.getTransaction().commit();
            return result.getId();
        }catch(Exception e)
        {
            if(session.getTransaction().isActive())
            {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return -1;
    }
    public List<Store> getAllStore()
    {
         try{
            session.getTransaction().begin();
            String sql = "from Store";
            Query query = session.createQuery(sql);
            List<Store> result = (List<Store>)query.list();
            session.flush();
            session.getTransaction().commit();
            return result;
        }catch(Exception e)
        {
            if(session.getTransaction().isActive())
            {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
    public Store getStoreById(int id)
    {
         try{
            session.getTransaction().begin();
            String sql = "from Store where id = ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, id);
            Store result = (Store)query.uniqueResult();
            session.flush();
            session.getTransaction().commit();
            return result;
        }catch(Exception e)
        {
            if(session.getTransaction().isActive())
            {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
}
